const isWeex = weex.config.env.platform.toLowerCase() !== 'web';
const nav = weex.requireModule('navigator');


export default {
    ballSort(arr) {
        return arr.sort(sortNumber)
    },
    lottoSwitchNumberType(value) {
        let typeString = ['直选', '组三单式', '组三复式', '组六', '排五直选', '胆拖', '复式', '单式']

        return value > 8 ? '未知类型' : typeString[value-1]

    },
    lottoSwitchnames(value) {
        let typeString = ['双色球', '超级大乐透', '福彩3D', '排列三', '排列五', '竞彩足球']

        return value > 6 ? '未知彩种' : typeString[value-1]

    },
    lottoSwitchtypes(value) {
        let typeString = ['union-lotto', 'super-lotto', 'lotto3d', 'paiThree', 'paiFive']

        return value > 5 ? 'super-lotto' : typeString[value-1]
    }
}


export const serialize = (obj) => {
    let str = ""

    for (let key in obj) {
        str += `${key}=${encodeURIComponent(obj[key])}&`
    }

    str = str.slice(0, -1)

    return str
}




export let push = (path, option) => {
    const toUrl = weex.config.bundleUrl.split('/').slice(0, -1).join('/') + '/dist/' + path.replace(/^\//, '') + '.js';// 将a.js的绝对地址转为b.js的绝对地址

    nav.push({
        url: toUrl + serialize(option),
        animated: 'true',
    })


}


export let query = (name) => {
    let param = weex.config.bundleUrl.split('?')[1].split('&')
    let obj = {}

    param.map(item => {
        let istr = item.split('=')
        obj[istr[0]] = istr[1]
    })

    return obj[name]
}



exports.getBaseURL = function (vm) {
    var bundleUrl = weex.config.bundleUrl;
    var nativeBase;
    var isAndroidAssets = bundleUrl.indexOf('your_current_IP') >= 0 || bundleUrl.indexOf('file://assets/') >= 0;
    var isiOSAssets = bundleUrl.indexOf('file:///') >= 0 && bundleUrl.indexOf('WeexDemo.app') > 0;
    if (isAndroidAssets) {
        nativeBase = 'file://assets/';
    }
    else if (isiOSAssets) {
        nativeBase = bundleUrl.substring(0, bundleUrl.lastIndexOf('/') + 1);
    }
    else {
        var host = 'localhost:8081';
        var matches = /\/\/([^\/]+?)\//.exec(weex.config.bundleUrl);
        if (matches && matches.length >= 2) {
            host = matches[1];
        }

        nativeBase = 'http://' + host + '/dist/';
    }

    var h5Base = vm.dir + '/dist/';
    // in Native
    var base = nativeBase;

    // if (typeof window === 'object') {
    //   // in Browser or WebView
    //   base = h5Base;
    // }
    return base
}


function sortNumber(a, b) {
    return a - b
}


// postResponsesToken